package com.dzzw.Dao;

import com.dzzw.Model.Message;
import com.dzzw.utils.DButils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//聊天信息
public class MessageDao {
    private final Connection con = DButils.getCon();
    //新增聊天记录
    public void MessageAdd(Message message){
        Date time= new java.sql.Date(new java.util.Date().getTime());
        String sql = "insert into t_message(employee_id,message_info,message_sendtime) value (?,?,?)";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1, message.getEmployee_id());
            preparedStatement.setString(2, message.getMessage_info());
            preparedStatement.setDate(3, (java.sql.Date)time);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        } catch(Exception e) {
            System.out.println("数据添加失败！");
            e.printStackTrace();
        }
    }

    //删除聊天记录
    public void MessageDelete(int message_id) {
        String sql = "Delete from t_message where message_id = ?";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,message_id);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch(Exception e){
            System.out.println("数据单条删除失败！");

            e.printStackTrace();
        }
    }

    //根据公务员id查询聊天记录
    public static List<Message> SelectMessageByEmployeeId(int employee_id){
        List<Message> list = new ArrayList<>();
        String sql = "SELECT m.message_id,e.employee_name,m.message_info,m.message_sendtime FROM t_message AS m INNER JOIN t_employee AS e ON m.employee_id = e.employee_id where m.employee_id = ?";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,employee_id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Message message = new Message();
                message.setMessage_id(resultSet.getInt(1));
                message.setEmployee_name(resultSet.getString(2));
                message.setMessage_info(resultSet.getString(3));
                message.setMessage_sendtime(resultSet.getDate(4));
                list.add(message);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
            System.out.println("查询所有数据成功！");
        }catch(Exception e){
            System.out.println("查询所有数据失败！");
            e.printStackTrace();
        }
        return list;
    }

    //查询所有记录
    public static List<Message> SelectAllMessage(){
        List<Message> list = new ArrayList<>();
        String sql = "SELECT m.message_id,e.employee_name,m.message_info,m.message_sendtime FROM t_message AS m INNER JOIN t_employee AS e ON m.employee_id = e.employee_id";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Message message = new Message();
                message.setMessage_id(resultSet.getInt(1));
                message.setEmployee_name(resultSet.getString(2));
                message.setMessage_info(resultSet.getString(3));
                message.setMessage_sendtime(resultSet.getDate(4));
                list.add(message);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
            System.out.println("查询所有数据成功！");
        }catch(Exception e){
            System.out.println("查询所有数据失败！");
            e.printStackTrace();
        }
        return list;
    }

}
